<!DOCTYPE html>
<?php
include('_includes/lock.php');
include ('_class/Navigator.php');
include_once('_class/DataBaseConnector.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $db = new DataBaseConnector();
    $db->connect();
    $id = addslashes($_POST['id']);
    $title = addslashes($_POST['title']);
    $bodytext = addslashes($_POST['bodytext']);
    $db->updateDataWhere("articles", "bodytext='$bodytext',title='$title'", "id=$id");
    header("location: entry.php?id=$id");
}

function showContent() {

    $id = $_GET['id'];
    if ($id > 0) {
        $db = new DataBaseConnector();
        $db->host = 'localhost';
        $db->username = 'pw_user';
        $db->password = 'pw';
        $db->database = 'pw';
        $db->connect();
        $selectedData = $db->selectDataWhere("articles", "title,bodytext", "id=$id");

        if ($selectedData != NULL) {
            $row = mysql_fetch_assoc($selectedData);
            $content = $row['bodytext'];
            $title = $row['title'];
            return "<form method=\"POST\">
                        <input name=\"id\" id=\"id\" type=\"text\" value=\"$id\" hidden=\"true\"/><br/>
                        <label for=\"title\">Title:</label><br/>
                        <input name=\"title\" id=\"title\" type=\"text\" maxlength=\"150\" value=\"$title\" class=\"large text\"/><br/>
                        <label for=\"bodytext\">Body Text:</label><br/>
                        <textarea name=\"bodytext\" id=\"bodytext\" rows=\"10\" cols=\"70\">$content</textarea><br/>
                        <input type=\"submit\" value=\"Save\" class=\"medium blue button\"/>
                    </form>";
        } else {
            return "Not Found";
        }
    }
}
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" href="resources/styles.css" type="text/css" media="screen" />
        <title>Edit Entry</title>
    </head>
    <body>
        <div id="wrapper">
            <header></header>
            <?php include ('_includes/nav.php') ?>
            <div id="page">
                <div id="content">
                    <?php echo showContent() ?>
                </div>
                <?php include ('_includes/sidebar.php') ?>
            </div>
            <footer>
                <section>
                    <p>Copyright ©2012 - All Rights Reserved</p>
                </section>
            </footer>
        </div>
    </body>
</html>
